package flex.cc.store.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import flex.cc.PublicUtil;
import flex.cc.nursing.dto.entity.AccountDataEntity;
import flex.cc.nursing.service.AccountDataService;
import flex.cc.store.dto.entity.StorePackageEntity;
import flex.cc.store.enums.PackageStatusEnum;
import flex.cc.store.service.PackageExportService;
import flex.cc.store.service.StorePackageService;
import flex.cc.util.ExportUtil;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
*
* @author KSY
* 2022年1月4日
* @desc 服务包导出
*/
@Service("packageExportService")
public class PackageExportServiceImpl implements PackageExportService {

	@Resource
    private StorePackageService storePackageService;
	@Resource
    private AccountDataService accountDataService;
	
	/**
	* @desc 服务包导出 KSY
	* @param flag: 1部分导出；2全部导出
	*/
	@SuppressWarnings("deprecation")
	@Override
	public void downLoadSelection(HttpServletRequest request, HttpServletResponse response, StorePackageEntity storePackageVo, String fileParentPath, String fileName, String flag) {
		// 表名、表头数据
		String title = fileName;
		String[] headers = {"服务包序列码","第三方服务包序列码","服务包名称","渠道方","服务商","服务包状态","发送时间","服务包生效时间","服务包失效时间","归属订单号","绑定车牌号","创建时间"};
		String[] fields = {"packageCodeCF","packageCode","packageName","channel","server","status","packSendDate","packEffDate","packExpDate","orderId","carNo","packCreateTime"};

		IPage<StorePackageEntity> page = null;
		if("1".equals(flag)){
			page = storePackageService.queryPackageListWithDataIds(storePackageVo);
		}else{
			page = storePackageService.queryPackageListWithPage(storePackageVo);
		}
		List<StorePackageEntity> records = page.getRecords();
		for(int i=0; i<records.size(); i++){
			StorePackageEntity storePackageEntity = records.get(i);
			//序列码类型
			storePackageEntity.setSeqType("1".equals(storePackageEntity.getSeqType())? "自营" : "第三方" );
			//服务包状态
			if(PublicUtil.isNotEmpty(storePackageEntity.getStatus())){
				storePackageEntity.setStatus(PackageStatusEnum.getName(storePackageEntity.getStatus()));
			}
			storePackageEntity.setChannel( storePackageEntity.getChannel().equals("ZXHY") ? "阳光" : storePackageEntity.getChannel() );
		}
		if (!PublicUtil.isEmpty(records)) {
			try {
				SXSSFWorkbook wb = ExportUtil.createWorkbook(10, false);//创建工作簿对象
				ExportUtil.exportExcel(title, headers, fields, 0, wb, records);
				File fileParent = new File(fileParentPath);
				if (!fileParent.exists()) { // 若目录不存在则创建
					fileParent.mkdirs();
				}

				File file = new File(fileParentPath + File.separator + fileName + ".xlsx");
				if (file.exists()) { // 若文件已存在则删除
					file.delete();
				}
				try {
					wb.write(new FileOutputStream(file));
				} catch (Exception e) {
					e.printStackTrace();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 *
	 * @author WANGCONG
	 * 2022年3月22日下午14:55:01
	 * @desc 核算数据导出
	 */
	@Override
	public void downLoadAccountData(HttpServletRequest request, HttpServletResponse response,
			AccountDataEntity accountData, String fileParentPath, String fileName) {
		// 表名、表头数据
		String title = fileName;
		String[] headers = { "渠道编码", "接口标识", "操作状态", "操作时间", "商品编码", "服务项目编号", "唯一标识", "订单号"};
		String[] fields = { "source", "apiSource", "cardStatus", "operateTime", "pid", "serviceItemCode","uniqueNo", "orderId"};

		List<AccountDataEntity> records = accountDataService.queryList(accountData);
		for (int i = 0; i < records.size(); i++) {
			AccountDataEntity data = records.get(i);
//			data.setSource(data.getSource().equals("ZXHY") ? "阳光" : data.getSource());
			data.setOrderId(PublicUtil.isNotEmpty(data.getOrderId()) ? data.getOrderId() : "");
		}
		try {
			SXSSFWorkbook wb = ExportUtil.createWorkbook(10, false);// 创建工作簿对象
			ExportUtil.exportExcel(title, headers, fields, 0, wb, records);
			File fileParent = new File(fileParentPath);
			if (!fileParent.exists()) { // 若目录不存在则创建
				fileParent.mkdirs();
			}
			File file = new File(fileParentPath + File.separator + fileName + ".xlsx");
			if (file.exists()) { // 若文件已存在则删除
				file.delete();
			}
			try {
				wb.write(new FileOutputStream(file));
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
